<?php
/***
* @version $Id: search.php 336 2007-01-23 08:12:56Z flexiondotorg $
* @copyright (c) 2006 - 2007 Flexion.Org
*            (c) 2004 - 2005 Project Minerva
*            (c) 2001 - 2006 phpBB Group
* @license   http://opensource.org/licenses/gpl-license.php GNU Public License
***/

define('IN_R3BORN', true);
$root_path = './';
$phpEx = substr(strrchr(__FILE__, '.'),	1);
include($root_path . 'common.'.$phpEx);

include($root_path . 'includes/class_search.'.$phpEx);

//
// Start session management
//
$userdata = session_pagestart($user_ip, PAGE_SEARCH);
init_userprefs($userdata);
//
// End session management
//

//
// Define initial vars
//
$mode = get_var('mode', '');
$search = get_var('search', '');
$search_keywords = get_var('search_keywords', '');
$search_hits = get_var('search_hits', 10);
if ($search_hits > 25)
{
	$search_hits = 25;
}

if ( isset($_POST['search_terms']) )
{
	$search_terms = ( $_POST['search_terms'] == 'all' ) ? 1 : 0;
}
else
{
	$search_terms = 0;
}

if ( isset($_POST['search_fields']) )
{
	$search_fields = ( $_POST['search_fields'] == 'all' ) ? 1 : 0;
}
else
{
	$search_fields = 0;
}

loadHooks(getHooks('search'));

//if an advanced search was requested, rediect to the appropriate module with any keywords.
if ($search != '')
{
	$search_keywords = ($search_keywords) ? '&amp;search_keywords=' . $search_keywords : '';
	redirect(str_replace('&amp;', '&', $search . $search_keywords));
}

if ($mode == 'searchcolor')
{
	//
	// This handles the simple windowed user color
	//
	color_search();
	exit;
}
elseif ( $mode == 'searchuser' )
{
	//
	// This handles the simple windowed user search functions called from various other scripts
	//
	if ( isset($_POST['search_username']) )
	{
		username_search($_POST['search_username']);
	}
	else
	{
		username_search('');
	}
	exit;
}
elseif ( $mode == 'quicksearch')
{
	$current_time = time();
	//
	// Flood control
	//
	$where_sql = ($userdata['user_id'] == ANONYMOUS) ? "se.session_ip = '$user_ip'" : 'se.session_user_id = ' . $userdata['user_id'];
	$sql = 'SELECT MAX(sr.search_time) AS last_search_time
		FROM ' . SEARCH_TABLE . ' sr, ' . SESSIONS_TABLE . " se
		WHERE sr.session_id = se.session_id
			AND $where_sql";
	if ($result = $db->sql_query($sql))
	{
		if ($row = $db->sql_fetchrow($result))
		{
			if (intval($row['last_search_time']) > 0 && ($current_time - intval($row['last_search_time'])) < intval($config['search_flood_interval']))
			{
				message_die(GENERAL_MESSAGE, $lang['Search_Flood_Error']);
			}
		}
	}

	$hooks->execute('quick-search', $search_keywords, $search_terms, $search_fields, $search_hits);

	//
	// Delete old data from the search result table
	//
	$sql = 'DELETE FROM ' . SEARCH_TABLE . '
			WHERE search_time < ' . ($current_time - (int) $config['session_length']);
	if ( !$result = $db->sql_query($sql) )
	{
		message_die(GENERAL_ERROR, 'Could not delete old search id sessions', '', __LINE__, __FILE__, $sql);
	}
}
else
{
	//display the search form not quick results.
	$template->assign_block_vars('switch_quicksearch_form',array());
}

page_header($lang['Search']);

$s_search_hits = '';
for($i = 5; $i < 26 ; $i += 5)
{
	$selected = ( $i == 10 ) ? ' selected="selected"' : '';
	$s_search_hits .= '<option value="' . $i . '"' . $selected . '>' . $i . '</option>';
}

$search_name = array();
$search_url  = array();

$hooks->execute('search-urls');

if (empty($search_url))
{
	$message = $lang['Search_nothing_to_search'] . "<br /><br />" . sprintf($lang['Click_return_admin_index'], "<a href=\"" . append_sid("index.$phpEx?pane=right") . "\">", "</a>");
	message_die(GENERAL_MESSAGE, $message);
}

$select_search = '<select name="search" onchange="if(this.options[this.selectedIndex].value != -1){ forms[\'searchbox\'].submit() }"><option value="-1">' . $lang['Select_module'] . '</option>';

for($i = 0; $i < count($search_name); $i++)
{
	$select_search .= '<option value="' . $search_url[$i] . '">' . $search_name[$i] . '</option>';
}
$select_search .= '</select>';

$template->set_filenames(array(
	'body' => 'search_site.tpl'
	)
);

$template->assign_vars(array(
	'L_SEARCH_QUERY' => $lang['Search_query'],
	'L_SEARCH_KEYWORDS' => $lang['Search_keywords'],
	'L_SEARCH_KEYWORDS_EXPLAIN' => $lang['Search_keywords_explain'],
	'L_SEARCH_ANY_TERMS' => $lang['Search_for_any'],
	'L_SEARCH_ALL_TERMS' => $lang['Search_for_all'],
	'L_RETURN_FIRST' => $lang['Return_first'],
	'L_SEARCH_HITS' => $lang['search_hits'],
	'S_SEARCH_HITS' => $s_search_hits,
	'L_SEARCH_MESSAGE_ONLY' => $lang['Search_msg_only'],
	'L_SEARCH_MESSAGE_TITLE' => $lang['Search_title_msg'],

	'L_GO' => $lang['Go'],
	'L_SEARCH_SITE' => $lang['Search_site'],
	'S_SEARCH_SELECT' => $select_search,
	'S_QUICK_SEARCH_ACTION' => append_sid('search.' . $phpEx . '?mode=quicksearch'),
	'S_ADVANCED_SEARCH_ACTION' => append_sid('search.' . $phpEx),
	)
);

$template->pparse('body');

page_footer();
?>